package com.jokerpan.project.controller;

import com.jokerpan.project.annotation.AccessLimit;
import com.jokerpan.project.annotation.LogAction;
import com.jokerpan.project.commons.ActionResult;
import com.jokerpan.project.commons.ListResult;
import com.jokerpan.project.commons.ResultBuilder;
import com.jokerpan.project.entity.Route;
import com.jokerpan.project.entity.Users;
import com.jokerpan.project.service.RouteService;
import com.jokerpan.project.util.UserTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;

@RestController
@RequestMapping("route")
@Api("路由操作Api")
public class RouteController {

    @Resource
    RouteService routeService;
    @Resource
    UserTool userTool;

    @GetMapping("/queryList")
    @AccessLimit(seconds = 6, maxCount = 5)
    @ApiOperation("查询所有路由分页")
    @PreAuthorize("hasPermission('','admin:list')")
    public ListResult<Object> queryList(Route route) {
        return ResultBuilder.buildListSuccess(this.routeService.queryList(route));
    }

    @GetMapping("/queryAll")
    @AccessLimit(seconds = 6, maxCount = 5)
    @ApiOperation("根据角色查询所有路由菜单")
    @PreAuthorize("hasPermission('','user:list')")
    public List<LinkedHashMap<String, Object>> queryAll() {
        Users users = userTool.getCurrentUser();
        return this.routeService.queryAll(users.getRoleId());
    }

    @PostMapping("/add")
    @AccessLimit(seconds = 6, maxCount = 5)
    @ApiOperation("新增路由")
    @LogAction("路由管理,新增路由数据,新增路由,新增")
    @PreAuthorize("hasPermission('','admin:update')")
    public ActionResult addRoute(@RequestBody Route route){
        if (this.routeService.add(route)){
            return ResultBuilder.buildActionSuccess();
        }else {
            return ResultBuilder.buildActionFailed();
        }
    }

    @PostMapping("/update")
    @AccessLimit(seconds = 6, maxCount = 5)
    @ApiOperation("修改路由")
    @LogAction("路由管理,修改路由数据,修改路由,修改")
    @PreAuthorize("hasPermission('','admin:update')")
    public ActionResult update(@RequestBody Route route){
        return this.routeService.update(route);
    }

    @GetMapping("/delete")
    @AccessLimit(seconds = 6, maxCount = 5)
    @ApiOperation("删除路由")
    @LogAction("路由管理,删除路由数据,删除路由,删除")
    @PreAuthorize("hasPermission('','admin:delete')")
    public ActionResult delete(@RequestParam("id") Integer id){
        return this.routeService.deleteById(id);
    }
}
